import requests
import os
# 在文件顶部添加类型提示
from lxml import etree  # type: ignore


# 创建img文件夹
os.makedirs('img', exist_ok=True)
url = 'https://movie.douban.com/top250'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
print(response.text)
tree= etree.HTML(response.text)
title_list = tree.xpath('//span[@class="title"][1]/text()')
img_list = tree.xpath('//img[@width="100"]/@src')

'''
如果想要获取标签中的文本要加上text()
如果想要获取标签中的属性值要加上@属性名

'''
for title,img in zip(title_list,img_list):
    item =dict()
    item['title'] = title
    item['img'] = img


    try:
        img_data = requests.get(img, headers=headers).content
        # 使用电影标题作为文件名，替换掉非法字符
        # 构建图片保存的路径，将图片存储在img文件夹下，以电影标题命名，文件格式为jpg
        img_path = f'img/{title}.jpg'
        with open(img_path, 'wb') as f:
            f.write(img_data)
        print(f'图片已保存到: {img_path}')
    except Exception as e:
        print(f'下载图片失败: {img}, 错误: {e}')